import store from '@/store'

export default {
  inserted(el, binding, vnode) {
    const { value } = binding
    const roles = store.getters && store.getters.roles
    // const elVal = vnode.context.$route.meta.buttonList;
    const elVal = store.getters.user.permissions_menu_button;
    if (value && value instanceof Array && value.length > 0) {
      const permissionRoles = value
      const hasPermission = elVal.some(role => {
        return permissionRoles.includes(role.permission_mark)
        // return permissionRoles.includes(role)
      })
      if (!hasPermission) {
        el.parentNode && el.parentNode.removeChild(el)
      }
    } else {
      throw new Error(`使用方式： v-permission="['admin','editor']"`)
    }

    // elVal.forEach(p => {
    //   if (!value.includes(p.permission_mark)) {
    //     el.parentNode && el.parentNode.removeChild(el) || (el.style.display = 'none')
    //   }
    // })
  }
}
